<template>
  <div>
    <a-result
      status="success"
      title="登陆成功，跳转中..."
      sub-title="估计2秒跳转。。"
    >
      <template #extra>
        <a-spin :spinning="isLoading" />
      </template>
    </a-result>
  </div>
</template>

<script>
import userApi from '../../api/myuser'
import { setToken } from '../../utils/auth'
export default {
  name: 'Qqauth',
  data() {
    return {
      linkDisabled: false,
      isLoading: true
    }
  },
  created() {
    // 'http://www.likunkun.club/#/qqauth#access_token=479FA9973698B0672C510D2606F86128&expires_in=7776000&state=fyuikjqytfgad'
    const href = window.location.href
    // 只有路径中包含#access_token才会进行后面的逻辑
    if (href.indexOf('#access_token') >= 0) {
      const lastIndex = href.lastIndexOf('&')
      const state = href.substr(lastIndex + 7)
      const index = href.trim().lastIndexOf('#')
      const substr = href.substr(index + 1)
      const index2 = substr.indexOf('&')
      // 取到 access_token=479FA9973698B0672C510D2606F86128 这个字符串
      const substr1 = substr.substr(0, index2)
      const access_token = 'access_token='
      const s = substr1.substr(access_token.length, substr1.length)
      if (s.length > 0) {
        // QQ
        if (!state.startsWith('admin_')) {
          userApi.qqLogin(s, '1').then(res => {
            if (res.code === 200) {
              this.$store.commit('SET_USER', res.data.user)
              setToken(res.data.token)
              this.isLoading = false
              if (!state.startsWith('login_')) {
                this.$router.push(`/info/${state}`)
              } else {
                this.$router.push('/')
              }
              this.$message.success(res.msg)
            } else {
              this.$message.error(res.msg)
            }
          })
          // 后台登录
        }
      } else {
        console.error('QQ登录accessToken解析异常', s)
      }
    } else {
      console.error('路径中没有包含#access_token')
    }
  }
}
</script>

<style scoped>

</style>
